//
//  MeTableViewController.m
//  IMChat
//
//  Created by Admin on 2021/6/28.
//  Copyright © 2021年 TeamOne. All rights reserved.
//

#import "MeTableViewController.h"
#import "ModifyPswViewController.h"
#import "modifyNameViewController.h"
#import "sexViewController.h"

@interface MeTableViewController ()

@end

@implementation MeTableViewController

//已经布局子视图
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    //刷新cell
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    /** 获得全局变量. */
    AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
            cell.textLabel.text = myDelegate.user.userName;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"用户ID：%@          性别：%@",myDelegate.user.userId,myDelegate.user.userSex];
            cell.detailTextLabel.textColor = [UIColor grayColor];
            
            cell.imageView.image = [UIImage imageNamed:@"touxiangme"];
            //调整图片大小
            CGSize itemSize = CGSizeMake(60, 60);
            UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
            CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
            [cell.imageView.image drawInRect:imageRect];
            cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
        }
        else if (indexPath.row == 1) {
            cell.textLabel.text = @"性别";
        }
        else if (indexPath.row == 2) {
            cell.textLabel.text = @"修改昵称";
        }
        else {
            cell.textLabel.text = @"修改密码";
        }
    }
    else if(indexPath.section == 1) {
        cell.textLabel.text = @"设置";
    }
    else {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.text = @"退出登录";
        cell.textLabel.font = [UIFont systemFontOfSize:21];
        cell.textLabel.textColor = [UIColor colorWithRed:0.502 green:0.502 blue:1 alpha:1.00];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    if (indexPath.section==2) {
        cell.accessoryType=UITableViewCellAccessoryNone;
    }else{
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}

//设置每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 88;
        }
        return 55;
    }
    return 55;
    
}

//选中哪一行cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //选中头像时
    if (indexPath.section == 0 && indexPath.row == 0) {
        NSLog(@"你选中了头像！");
        
    }
    //选中修改性别
    else if (indexPath.section == 0 && indexPath.row == 1) {
        NSLog(@"你选中了修改性别！");
        
        // 隐藏底部tarbar
        self.tabBarController.tabBar.hidden = YES;
        //显示页面
        sexViewController *sexPick = [[sexViewController alloc] init];
        [self.navigationController pushViewController:sexPick animated:YES ];
    }
    //选中修改昵称
    else if (indexPath.section == 0 && indexPath.row == 2) {
        NSLog(@"你选中了修改昵称！");
        
        // 隐藏底部tarbar
        self.tabBarController.tabBar.hidden = YES;
        //显示页面
        modifyNameViewController *mdfName = [[modifyNameViewController alloc] init];
        [self.navigationController pushViewController:mdfName animated:YES ];
    }
    //选中修改密码
    else if (indexPath.section == 0 && indexPath.row == 3) {
        NSLog(@"你选中了修改密码！");
        // 隐藏底部tarbar
        self.tabBarController.tabBar.hidden = YES;
        //显示页面
        ModifyPswViewController *mdfname = [[ModifyPswViewController alloc] init];
        [self.navigationController pushViewController:mdfname animated:YES ];
    }
    //选中设置
    else if (indexPath.section == 1) {
        NSLog(@"你选中了设置！");
    }
    //选中退出登录
    else {
        NSLog(@"你选中了退出登录！");
        //断开融云连接
        [[RCIM sharedRCIM] disconnect:YES];

        //删除Keychain信息
        //[SAMKeychain deletePasswordForService:@"userId" account:AppKey];
        [SAMKeychain deletePasswordForService:@"userPassword" account:AppKey];
        [SAMKeychain deletePasswordForService:@"token" account:AppKey];
        
        //跳转注册登录窗口
        LoginViewController *login = (LoginViewController *)[[UIStoryboard storyboardWithName:@"Login" bundle:nil]instantiateInitialViewController];
        [self presentViewController:login animated:NO completion:nil];
        
        //dimiss窗口
        //[self dismissViewControllerAnimated:NO completion:nil];
    }
    
    
}


@end

